<template>
  <div class="serial-editor-wrap">
    <div class="editor-header">
      <h5>{{$t('config.ruleInfo')}}</h5>
    </div>
    <div class="editor-content">
      <g-detail ref="detail" :inputConfig="inputConfig" :rules="rules" @changeDetail="changeDetail"></g-detail>
    </div>
  </div>
</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
export default {
  name:"serialEditor",
mixins: [baseMixin],
  props:{
    id:{
      type:Number
    },
    data:{
      type:Object
    }
  },
  data(){
    return {
      detail:{
        enabledYear:1,
        enabledMonth:1,
        enabledDay:1,
      },
      inputConfig:[
        {
            type: "select",
            label: this.$t('config.sysCodeType'),
            props: "numberType",
            options: this.getOptions("codelkup/NumberType").map(opt => {opt.code = parseInt(opt.code);return opt}),
            disabled: this.id > 0,
          },
          {
            type: "select",
            label: this.$t('config.sysCodeLevel'),
            defaultValue:this.$store.state.wms.toUpperCase().includes('WMWHSE') ? 11802 : 11801,
            props: "numberLevel",
            options: this.getOptions('codelkup/NumberLevel').map(opt => {opt.code = parseInt(opt.code);return opt}),
            disabled: true
          },
          {
            type: "select",
            label: this.$t('config.appendChar'),
            props: "appendChar",
            options: this.getOptions('codelkup/AppendChar').map(opt => {opt.code = parseInt(opt.code);return opt}),
            break:true
          },
          {
            type: "input",
            label: this.$t('config.numberPrefix'),
            props: "numberPrefix",
            break:true
          },
          {
            type: "select",
            label: this.$t('config.yearCount'),
            props: "yearCount",
            defaultValue:11901,
            options: this.getOptions('codelkup/YearCount').map(opt => {opt.code = parseInt(opt.code);return opt}),
            disabled: false,
          },
          {
            type: "switch",
            label: this.$t('config.enabledYear'),
            props: "enabledYear",
            defaultValue:1,
            break:true
          },
          {
            type: "select",
            label: this.$t('config.monthAddZero'),
            props: "monthAddZero",
            defaultValue:1,
            options: [{ code: 0, description: this.$t('code.no') }, { code: 1, description: this.$t('code.yes') }],
            disabled: false,
          },
          {
            type: "switch",
            label: this.$t('config.enabledMonth'),
            props: "enabledMonth",
            defaultValue:1,
            break:true
          },
          {
            type: "select",
            label: this.$t('config.dayAddZero'),
            props: "dayAddZero",
            defaultValue:1,
            options: [{ code: 0, description: this.$t('code.no') }, { code: 1, description: this.$t('code.yes') }],
            disabled: false,
          },
          {
            type: "switch",
            label: this.$t('config.enabledDay'),
            props: "enabledDay",
            defaultValue:1,
            break:true
          },
          {
            type: "select",
            label: this.$t('config.serialType'),
            props: "serialType",
            defaultValue:12101,
            options: this.getOptions('codelkup/SerialType').map(opt => {opt.code = parseInt(opt.code);return opt}),
          },
          {
            type: "input",
            inputType:"number",
            label: this.$t('config.serialCount'),
            props: "serialCount",
            defaultValue:4,
            break:true
          },
          {
            type: "textarea",
            label:  this.$t('app.remark'),
            props: "remark",
            switch:true,
            break:true
          },
      ],
      rules: {
        numberType: [
          { required: true, message:this.$selectPlease("config.sysCodeType"), trigger: 'blur' },
        ],
        serialType: [
          { required: true},
        ],
        serialCount: [
          { required: true, message: this.$inputPlease("config.serialCount"), trigger: 'blur' },
        ],
      }
    }
  },
  methods:{
    changeDetail(detail,prop){
      this.inputConfig.find(item => item.props == prop).defaultValue = detail[prop]
      if(prop == "enabledYear"){
        if(detail[prop] != 1){
          this.inputConfig.find(item => item.props == "yearCount").disabled = true
          this.inputConfig.find(item => item.props == "yearCount").defaultValue = ""
          this.inputConfig.find(item => item.props == "monthAddZero").disabled = true
          this.inputConfig.find(item => item.props == "monthAddZero").defaultValue = ""
          this.inputConfig.find(item => item.props == "enabledMonth").defaultValue = 0
          this.inputConfig.find(item => item.props == "dayAddZero").disabled = true
          this.inputConfig.find(item => item.props == "dayAddZero").defaultValue = ""
          this.inputConfig.find(item => item.props == "enabledDay").defaultValue = 0
        }else{
          this.inputConfig.find(item => item.props == "yearCount").disabled = false
          this.inputConfig.find(item => item.props == "yearCount").defaultValue = 11901
        }
      }
      if(prop == "enabledMonth"){
        if(detail[prop] != 1){
          this.inputConfig.find(item => item.props == "monthAddZero").disabled = true
          this.inputConfig.find(item => item.props == "monthAddZero").defaultValue = ""
          this.inputConfig.find(item => item.props == "dayAddZero").disabled = true
          this.inputConfig.find(item => item.props == "dayAddZero").defaultValue = ""
          this.inputConfig.find(item => item.props == "enabledDay").defaultValue = 0
        }else{
          this.inputConfig.find(item => item.props == "monthAddZero").disabled = false
          this.inputConfig.find(item => item.props == "monthAddZero").defaultValue = 1
        }
      }
      if(prop == "enabledDay"){
        if(detail[prop] != 1){
          this.inputConfig.find(item => item.props == "dayAddZero").disabled = true
          this.inputConfig.find(item => item.props == "dayAddZero").defaultValue = ""
        }else{
          this.inputConfig.find(item => item.props == "dayAddZero").disabled = false
          this.inputConfig.find(item => item.props == "dayAddZero").defaultValue = 1
        }
      }
      if(prop == "serialType"){
        if(detail[prop] == 12101){
          this.inputConfig.find(item => item.props == "serialCount").defaultValue = 4
        }else{
          this.inputConfig.find(item => item.props == "serialCount").defaultValue = 9
        }
      }
      // if(prop == "serialCount"){
      //   if(detail.serialType == 12101&&(detail[prop]<4||detail[prop]>12)){
      //     this.$message("序号长度最小4位最大12位")
      //   }
      //   if(detail.serialType == 12102&&(detail[prop]<4||detail[prop]>12)){
      //     this.$message("序号长度最小9位最大12位")
      //   }
      // }
      this.$emit("update:data",detail)
    },
    getSerialInfo(){
      this.inputConfig.forEach((item,index) => {
        this.$set(this.inputConfig[index],'defaultValue',this.data[item.props])
        // item.defaultValue = this.data[item.props]
        this.changeDetail(this.data,item.props)
      })
    }
  },
  mounted(){
    if(this.id){
      this.getSerialInfo()
    }
  },
}
</script>

<style lang="scss" scoped>
.serial-editor-wrap{
  border: 1px solid #dddddd;
  .editor-header{
    padding: 5px;
    background: #eff3f7;
    border-bottom: 1px solid #dddddd;
  }
  h4{
    position: relative;
    padding-left: 1em;
    &::before{
      content: "";
      position: absolute;
      width: 4px;
      height: 1em;
      left: 0;
      background: #409eff;
    }
  }
  .editor-content{
    padding: 10px;
  }
}
</style>